package com.jf3q.gateway.security.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jf3q.gateway.security.entity.AclUser;
import com.jf3q.gateway.security.service.AclUserService;
import com.jf3q.gateway.security.mapper.AclUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 *
 */
@Service
public class AclUserServiceImpl extends ServiceImpl<AclUserMapper, AclUser>
    implements AclUserService, ReactiveUserDetailsService {

    @Resource
    AclUserMapper userMapper;

    @Override
    public Mono<UserDetails> findByUsername(String username) {

        AclUser aclUser = userMapper.getByUsername(username);
        if (aclUser == null ){
            return Mono.error(new UsernameNotFoundException("用户不存在"));
        }

        UserDetails userDetails = User.withUsername(username)
                .password(aclUser.getPassword())
                .authorities(aclUser.getAuthorities())
                .build();
        return Mono.just(userDetails);

    }
}




